package com.assignwork.vo;

import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import com.fasterxml.jackson.databind.ser.std.ToStringSerializer;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import org.springblade.core.tool.utils.DateUtil;
import com.fasterxml.jackson.annotation.JsonFormat;
import org.springblade.core.tool.utils.Func;
import org.springframework.format.annotation.DateTimeFormat;

/**
 * DynamicFormVO
 */
@Data
@ApiModel(value = "DynamicFormVO对象", description = "动态表单")
public class DynamicFormVO {

    /**
     * 主键id;主键id
     */
    @JsonSerialize(using = ToStringSerializer.class)
    @ApiModelProperty(value = "主键id")
    private Long id;

    /**
     * 创建时间
     */
    @DateTimeFormat(pattern = DateUtil.PATTERN_DATETIME)
    @JsonFormat(pattern = DateUtil.PATTERN_DATETIME)
    @ApiModelProperty(value = "创建时间")
    private LocalDateTime createTime;

    /**
     * 更新时间
     */
    @DateTimeFormat(pattern = DateUtil.PATTERN_DATETIME)
    @JsonFormat(pattern = DateUtil.PATTERN_DATETIME)
    @ApiModelProperty(value = "更新时间")
    private LocalDateTime updateTime;

    /**
     * 日期
     */
    @DateTimeFormat(pattern = DateUtil.PATTERN_DATE)
    @JsonFormat(pattern = DateUtil.PATTERN_DATE)
    @ApiModelProperty(value = "日期")
    private LocalDate formDate;

    /**
     * 类型
     */
    @ApiModelProperty(value = "类型")
    private String formType;

    /**
     * 行内容
     */
    @ApiModelProperty(value = "行内容")
    private String columnsKey;

    /**
     * 列内容
     */
    @ApiModelProperty(value = "列内容")
    private String rowsKey;

    private List<DynamicFormDataVO> data;

    private Map<String, Map<String, DynamicFormDataVO>> columnMap;

    private Map<String, Map<String, DynamicFormDataVO>> rowMap;

    public Map<String, Map<String, DynamicFormDataVO>> getRowMap() {
        if (Func.isNotEmpty(data)) {
            Map<String, Map<String, DynamicFormDataVO>> res = new HashMap<>();
            Map<String, List<DynamicFormDataVO>> map = data.stream().collect(Collectors.groupingBy(DynamicFormDataVO::getDataRow));
            for (Map.Entry<String, List<DynamicFormDataVO>> entry : map.entrySet()) {
                String key = entry.getKey();
                List<DynamicFormDataVO> value = entry.getValue();
                Map<String, DynamicFormDataVO> columnMap = value.stream().collect(Collectors.toMap(DynamicFormDataVO::getDataColumn, a -> a, (k1, k2) -> k1));
                res.put(key, columnMap);
            }
            return res;
        }
        return rowMap;
    }

    public Map<String, Map<String, DynamicFormDataVO>> getColumnMap() {
        if (Func.isNotEmpty(data)) {
            Map<String, Map<String, DynamicFormDataVO>> res = new HashMap<>();
            Map<String, List<DynamicFormDataVO>> map = data.stream().collect(Collectors.groupingBy(DynamicFormDataVO::getDataColumn));
            for (Map.Entry<String, List<DynamicFormDataVO>> entry : map.entrySet()) {
                String key = entry.getKey();
                List<DynamicFormDataVO> value = entry.getValue();
                Map<String, DynamicFormDataVO> rowMap = value.stream().collect(Collectors.toMap(DynamicFormDataVO::getDataRow, a -> a, (k1, k2) -> k1));
                res.put(key, rowMap);
            }
            return res;
        }
        return columnMap;
    }
}